package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QKnowkedgMapper;
import com.ruoyi.system.domain.QKnowkedg;
import com.ruoyi.system.service.IQKnowkedgService;
import com.ruoyi.common.core.text.Convert;

/**
 * 问题库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-11
 */
@Service
public class QKnowkedgServiceImpl implements IQKnowkedgService 
{


    private static final Logger log = LoggerFactory.getLogger(QKnowkedgServiceImpl.class);
    @Autowired
    private QKnowkedgMapper qKnowkedgMapper;

    /**
     * 查询问题库
     * 
     * @param knowledgId 问题库主键
     * @return 问题库
     */
    @Override
    public QKnowkedg selectQKnowkedgByKnowledgId(Long knowledgId)
    {
        return qKnowkedgMapper.selectQKnowkedgByKnowledgId(knowledgId);
    }

    /**
     * 查询问题库列表
     * 
     * @param qKnowkedg 问题库
     * @return 问题库
     */
    @Override
    public List<QKnowkedg> selectQKnowkedgList(QKnowkedg qKnowkedg)
    {
        return qKnowkedgMapper.selectQKnowkedgList(qKnowkedg);
    }

    @Override
    public List<QKnowkedg> selectQKnowkedgList1(List<String> ids)
    {
        return qKnowkedgMapper.selectQKnowkedgList1(ids);
    }

    /**
     * 新增问题库
     * 
     * @param qKnowkedg 问题库
     * @return 结果
     */
    @Override
    public int insertQKnowkedg(QKnowkedg qKnowkedg)
    {
        return qKnowkedgMapper.insertQKnowkedg(qKnowkedg);
    }

    /**
     * 修改问题库
     * 
     * @param qKnowkedg 问题库
     * @return 结果
     */
    @Override
    public int updateQKnowkedg(QKnowkedg qKnowkedg)
    {
        return qKnowkedgMapper.updateQKnowkedg(qKnowkedg);
    }

    /**
     * 批量删除问题库
     * 
     * @param knowledgIds 需要删除的问题库主键
     * @return 结果
     */
    @Override
    public int deleteQKnowkedgByKnowledgIds(String knowledgIds)
    {
        return qKnowkedgMapper.deleteQKnowkedgByKnowledgIds(Convert.toStrArray(knowledgIds));
    }

    /**
     * 删除问题库信息
     * 
     * @param knowledgId 问题库主键
     * @return 结果
     */
    @Override
    public int deleteQKnowkedgByKnowledgId(Long knowledgId)
    {
        return qKnowkedgMapper.deleteQKnowkedgByKnowledgId(knowledgId);
    }



    /**
     * 导knowlege表格数据
     *
     * @param questionList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importQuestion(List<QKnowkedg> quesitonList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(quesitonList) || quesitonList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
     //   String password = configService.selectConfigByKey("sys.user.initPassword");
        for (QKnowkedg question : quesitonList)
        {
            try
            {
                // 验证是否存在这个用户
                QKnowkedg u = qKnowkedgMapper.selectQKnowkedgByKnowledgId(question.getKnowledgId());
                if (StringUtils.isNull(u))
                {

                    qKnowkedgMapper.insertQKnowkedg(question);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + question.getKnowledgId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {

                    question.setKnowledgId(u.getKnowledgId());
                    question.setUpdateBy(operName);
                    qKnowkedgMapper.updateQKnowkedg(question);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、问题 " +question.getKnowledgId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " 问题 " + question.getKnowledgId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + question.getKnowledgId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }




}
